package com.qqtech.qquan.draw.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.community.model.CommunityBaseQuery;
import com.qqtech.qquan.community.model.CommunityBaseVo;
import com.qqtech.qquan.community.service.CommunityBaseService;
import com.qqtech.qquan.coupon.model.CouponBaseVo;
import com.qqtech.qquan.coupon.service.CouponBaseService;
import com.qqtech.qquan.draw.model.DrawBaseVo;
import com.qqtech.qquan.draw.model.DrawBatch;
import com.qqtech.qquan.draw.model.DrawBatchQuery;
import com.qqtech.qquan.draw.model.DrawBatchVo;
import com.qqtech.qquan.draw.model.DrawItemQuery;
import com.qqtech.qquan.draw.model.DrawItemVo;
import com.qqtech.qquan.draw.service.DrawBaseService;
import com.qqtech.qquan.draw.service.DrawBatchService;
import com.qqtech.qquan.draw.service.DrawItemService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/draw/batch")
public class DrawBatchController extends BaseAdminController {
	@Resource
	private DrawBatchService drawBatchService;

	@Resource
	private CouponBaseService couponBaseService;

	@Resource
	private DrawItemService drawItemService;

	@Resource
	private DrawBaseService drawBaseService;
	
	@Resource
	private CommunityBaseService communityBaseService;

	@Override
	protected BaseService<DrawBatch> getBaseService() {
		return drawBatchService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/draw/drawBatch-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(DrawBatchQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<DrawBatchVo> page = drawBatchService.queryPageList(query, pageKit);
		DrawBaseVo drawBaseVo = null;
		DrawItemVo drawItemVo = null;
		for (DrawBatchVo drawBatchVo : page) {
			drawBaseVo = drawBaseService.getById(drawBatchVo.getDrawBaseId());
			drawBatchVo.setDrawBaseName(drawBaseVo.getName());
			drawItemVo = drawItemService.getById(drawBatchVo.getDrawItemId());
			drawBatchVo.setDrawItemName(drawItemVo.getName());
			drawBatchVo.setDrawItemImg(drawItemVo.getPathImgs());
		}
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	/**
	 * 去新增
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(String drawItemIds, Model model) {
		String[] ids = drawItemIds.split(";");
		if (ids != null && ids.length > 0) {
			DrawItemQuery drawItemQuery = new DrawItemQuery();
			List<Integer> includeIds = new ArrayList<Integer>();
			for (String id : ids) {
				includeIds.add(Integer.valueOf(id));
			}
			if (includeIds.size() > 0) {
				drawItemQuery.setIncludeIds(includeIds);
				List<DrawItemVo> drawItems = drawItemService.queryList(drawItemQuery);
				CouponBaseVo couponBaseVo = null;
				for (DrawItemVo drawItemVo : drawItems) {
					if (null != drawItemVo.getCouponId()) {
						couponBaseVo = couponBaseService.getById(drawItemVo.getCouponId());
						drawItemVo.setQuantity(couponBaseVo.getQuantity());
					}
				}
				model.addAttribute("drawItems", drawItems);
			}
		}
		CommunityBaseQuery communityBaseQuery = new CommunityBaseQuery();
		communityBaseQuery.setIsOpen(YesNoEnum.YES.getCode());
		communityBaseQuery.setIsShow(YesNoEnum.YES.getCode());
		List<CommunityBaseVo> communityList = communityBaseService.queryList(communityBaseQuery);
		model.addAttribute("communityList", communityList);
		return "/draw/drawBatch-add";
	}

	@RequestMapping(value = "/toUpdate")
	public String toUpdate(Integer id, Model model) {
		DrawBatchVo detail = drawBatchService.getById(id);
		DrawItemVo drawItemVo=drawItemService.getById(detail.getDrawItemId());
		if(null!=drawItemVo.getCouponId()){
			CouponBaseVo couponBaseVo=couponBaseService.getById(drawItemVo.getCouponId());
			model.addAttribute("couponBaseVo", couponBaseVo);
		}
		detail.setDrawItemName(drawItemVo.getName());
		detail.setDrawItemImg(drawItemVo.getPathImgs());
		model.addAttribute("detail", detail);
		return "/draw/drawBatch-update";
	}
	
	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(DrawBatch drawBatch) {
		Result ret = new Result();
		DrawBatchVo detail = drawBatchService.getById(drawBatch.getId());
		DrawItemVo drawItemVo=drawItemService.getById(detail.getDrawItemId());
		if(null!=drawItemVo.getCouponId()){
			CouponBaseVo couponBaseVo=couponBaseService.getById(drawItemVo.getCouponId());
			if(null!=couponBaseVo){
				if(drawBatch.getQuantity()>couponBaseVo.getQuantity()){
					ret.addErr("奖品数量不能大于优惠卷数量");
					return ret;
				}
			}
		}
		drawBatchService.updateById(drawBatch);
		return ret;
	}
}
